<template>
  <view class="content">
    <van-nav-bar title="用户登录-童装库存网" :left-arrow="false" placeholder fixed />
    <view class="login">
      <view class="logo"> <image class="img-box" src="https://images.tongzhuangkc.com/tz/logo2%402x.png" /> </view>
      <view class="login-agreement">
        <van-checkbox :value="checked" @change="changeHandle">我已阅读并同意</van-checkbox
        ><text @click="goPage('service')">《服务协议》</text>和<text @click="goPage('privacy')">《隐私政策》</text>
      </view>
      <view class="login-opt">
        <view class="opt-text">快捷登录</view>
        <view class="opt-btn" v-if="!checked" @click="checkLogin"><text></text>微信授权登录</view>
        <button v-else class="opt-btn" type="default" open-type="getPhoneNumber" @getphonenumber="loginHandle">
          <text></text>微信授权登录
        </button>
      </view>
    </view>
  </view>
</template>

<script>
import { reactive, toRefs, onMounted } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { useUserStore } from '@/store/modules/user'
import { setStorage, getStorage } from '@/utils/auth'
import { login, getPhone } from '@/http/user'
const userStore = useUserStore()
export default {
  components: {},
  setup() {
    const state = reactive({
      checked: false,
      safeAreaTop: 0,
      safeAreaBot: 0,
    })
    const changeHandle = () => {
      state.checked = !state.checked
      setStorage('agreement', state.checked ? '1' : '0')
    }
    const checkLogin = () => {
      if (!state.checked) {
        uni.showToast({
          title: '请勾选协议与政策',
          icon: 'none',
        })
      }
    }
    // 点击微信授权 跳转公众号授权
    const loginHandle = (e) => {
      userStore.setCode(e.detail.code)
      if (e.detail.code) {
        uni.redirectTo({
          url: `/pages/wxlogin/index`,
        })
      }
    }
    // 获取到手机号、个人信息 进行登陆
    const empowerHandle = async (wxopenid) => {
      const { data } = await getPhone({
        code: userStore.code,
      })
      uni.showLoading({
        title: '正在登录中，请稍后',
        mask: true,
      })
      // 登录授权获取code码
      uni.login({
        provider: 'wexin',
        success: ({ code }) => {
          // 获取个人信息
          uni.getUserInfo({
            provider: 'weixin',
            success: async (info) => {
              const res = await login({
                code,
                iv: info.iv,
                encrypted_data: encodeURIComponent(info.encryptedData),
                wxopenid,
                phone: data.phone,
              })
              setStorage('Admin-Token', res.data.token)
              uni.switchTab({
                url: `/pages/home/index`,
              })
            },
          })
        },
      })
    }
    // 跳转
    const goPage = (type) => {
      uni.navigateTo({
        url: `/subpackage-agreement/${type}/index`,
      })
    }
    onLoad((e) => {
      state.checked = getStorage('agreement') === '1'
      if (e.mpOpenid) {
        empowerHandle(e.mpOpenid)
      }
    })
    onMounted(() => {
      uni.getSystemInfo({
        success: function (res) {
          state.safeAreaTop = res.safeAreaInsets.top === 20 ? 0 : res.safeAreaInsets.top
        },
      })
    })
    return {
      ...toRefs(state),
      changeHandle,
      loginHandle,
      checkLogin,
      goPage,
    }
  },
}
</script>

<style lang="scss">
.content {
  background: #fff;
  height: 100vh;
  text-align: center;
}
.login {
  text-align: center;
  .logo {
    margin: 218rpx 0 80rpx;
  }
  .img-box {
    width: 256rpx;
    height: 256rpx;
  }
}
.login-agreement {
  display: flex;
  justify-content: center;
  font-size: 28rpx;
  text {
    color: #3775f2;
  }
}
.login-opt {
  text-align: center;
  .opt-text {
    font-size: 28rpx;
    color: #7f8798;
    line-height: 40rpx;
    margin: 96rpx 0 48rpx;
  }
  .opt-btn {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 360rpx;
    height: 80rpx;
    margin: 0 auto;
    background: #57be6a;
    border-radius: 8rpx;
    font-size: 28rpx;
    color: #fff;
    font-weight: 500;
    text {
      display: inline-block;
      width: 48rpx;
      height: 48rpx;
      margin-right: 16rpx;
      background: url('https://images.tongzhuangkc.com/tz/weixin%201%402x.png') no-repeat;
      background-size: 100% 100%;
    }
  }
}
</style>
